package com.boolib.shopadmin.web;

import com.boolib.commons.Const;
import com.boolib.commons.entity.ServerResponse;
import com.boolib.pojo.SysUser;
import com.boolib.service.IAdminService;
import com.boolib.shopadmin.config.shiro.commons.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.lang.invoke.MethodType;

@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private IAdminService iAdminService;



    @RequestMapping(value = "/login",method =RequestMethod.GET)
    public String loginPage(SysUser sysUser){
        return "login";
    }



    @RequestMapping(value = "/login",method =RequestMethod.POST)
    public void login(SysUser sysUser){
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken();

        token.setUsername(sysUser.getUsername());

        token.setPassword(sysUser.getPassword().toCharArray());

        subject.login(token);
    }

//
//    @RequiresAuthentication
//    @RequestMapping(value = "/logout")
//    public void logout(){
//        Subject subject = SecurityUtils.getSubject();
//        subject.logout();
//    }



    /**
     * 需要拥有超级管理员权限 ，但是不能注册超级管理员
     * @param sysUser
     * @return 是否成功状态
     */
    @RequiresAuthentication
    @ResponseBody
    @RequestMapping(value = "/register")
    public ServerResponse sysUserRegister(SysUser sysUser){

        ServerResponse serverResponse = isLogin();

        SysUser sysUser1 = (SysUser) serverResponse.getData();

        sysUser.setCreateBy(sysUser1.getUsername());

        return iAdminService.sysUserRegister(sysUser);
    }



















    @ResponseBody
    @RequestMapping(value = "/forgetPassword")
    public ServerResponse sysUserForgetPassword(String username){
        return iAdminService.sysUserForgetPassword(username);
    }

    @ResponseBody
    @RequestMapping(value = "/alterPasswordBeforeLogin")
    public ServerResponse sysUserAlterPasswordBeforeLogin(String password,String code,HttpSession httpSession){

        SysUser sysUser = (SysUser) httpSession.getAttribute(Const.SysUser.SESSIONAUTH);

        return iAdminService.sysUserAlterPasswordBeforeLogin(sysUser.getId(),password,code);
    }



    @RequiresAuthentication
    @ResponseBody
    @RequestMapping(value = "/alterPasswordAfterLogin")
    public ServerResponse sysUserAlterPasswordAfterLogin(String password,HttpSession httpSession){
        SysUser sysUser = (SysUser) httpSession.getAttribute(Const.SysUser.SESSIONAUTH);
        return iAdminService.sysUserAlterPasswordAfterLogin(sysUser.getId(),password);
    }






    @RequiresAuthentication
    @ResponseBody
    @RequestMapping(value = "/selectUserAbnormal")
    public ServerResponse sysUserSelectShopUserAbnormal(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            HttpSession session){
        return  iAdminService.sysUserSelectShopUserAbnormal(pageNum,pageSize);

    }

    @RequiresAuthentication
    @ResponseBody
    @RequestMapping(value = "/selectUserNormal")
    public ServerResponse sysUserSelectShopUserNormal(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            HttpSession session){
        return  iAdminService.sysUserSelectShopUserNormal(pageNum,pageSize);
    }





    @RequiresAuthentication
    @ResponseBody
    @RequestMapping(value = "/selectSysUserNormal")
    public ServerResponse sysUserSelectSysUserNormal(
                             @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                             HttpSession session){
        return  iAdminService.sysUserSelectSysUserNormal(pageNum,pageSize);
    }







    @RequiresAuthentication
    @ResponseBody
    @RequestMapping(value = "/selectSysUserAbnormal")
    public ServerResponse sysUserSelectSysUserAbnormal(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            HttpSession session){
        return  iAdminService.sysUserSelectSysUserAbnormal(pageNum,pageSize);
    }







    private SysUser getSysUserBySession(HttpSession session){

        return  (SysUser)session.getAttribute(Const.SysUser.SESSIONAUTH);

    }


    private ServerResponse isLogin(){
      SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(Const.SysUser.SESSIONAUTH);

      if(sysUser == null){
          return  ServerResponse.createByErrorMessage("未登录");
      }
      return ServerResponse.createBySuccess(sysUser);

    }



}
